package com.rajesh.messageservice.helper;

import com.rajesh.messageservice.bean.Message;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ResourceSender {

    public static boolean post(List<Message> messages) {
        if(messages == null || messages.size() == 0)
            return false;

        boolean notResource = false;
        try{
            String mediaBasePath = GlobalConfig.MediaBasePath + File.separator + GlobalConfig.Identity;

            for (Message msg: messages) {
                String absolutePath;
                List<String> uploadFiles = new ArrayList<>();
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("msgSvrId", msg.getMsgSvrId());
                switch (msg.getType()){
                    case MessageType.PICTURE: {
                        hashMap.put("type", GlobalConfig.Image);
                        String fileName = msg.getImgPath().substring(GlobalConfig.Thumbnail_Dirpath.length());
                        absolutePath = mediaBasePath + File.separator + GlobalConfig.ImagePartial + File.separator + fileName.substring(3, 5) + File.separator + fileName.substring(5, 7);
                        //uploadFiles.add(absolutePath + File.separator + fileName);
                        uploadFiles.add(absolutePath + File.separator + fileName + "hd");
                        break;
                    }
                    case MessageType.VOICE: {
                        hashMap.put("type", GlobalConfig.Voice);
                        //转换编码 silk to mp3

                        String fileName = "msg_" + msg.getImgPath() + ".amr";
                        absolutePath = mediaBasePath + File.separator + GlobalConfig.VoicePartial;
                        absolutePath = FileHelper.findFile(fileName, absolutePath);
                        uploadFiles.add(absolutePath);
                        break;
                    }
                    case MessageType.VIDEO: {
                        hashMap.put("type", GlobalConfig.Video);
                        String fileName = msg.getImgPath() + ".mp4";
                        absolutePath = mediaBasePath + File.separator + GlobalConfig.VideoPartial;
                        absolutePath = FileHelper.findFile(fileName, absolutePath);
                        uploadFiles.add(absolutePath);
                        break;
                    }
                    case MessageType.EMOJI: {
                        hashMap.put("type", GlobalConfig.Emoji);
                        String fileName = msg.getImgPath();
                        absolutePath = mediaBasePath + File.separator + GlobalConfig.EmojiPartial;
                        absolutePath = FileHelper.findFile(fileName, absolutePath);
                        uploadFiles.add(absolutePath);
                        break;
                    }
                    case MessageType.APP: {
                        return AppMsgHandler.handleMsg(msg);
                    }
                    default: {
                        continue;
                    }
                }
                notResource &= FileHelper.uploadFile(GlobalConfig.FileServerUrl, uploadFiles, hashMap);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return notResource;
    }
}

